package leetcode.primary;

public class T121 {
    public int maxProfit(int[] prices) {
        int minIndex = 0;
        int maxProfit = 0;
        for (int i = 1; i < prices.length; i++) {
            if (prices[i]-prices[minIndex]>maxProfit){
                maxProfit = prices[i] - prices[minIndex];
            }
            if (prices[i]<prices[minIndex]){
                minIndex = i;
            }
        }

        return maxProfit;
    }

    public static void main(String[] args) {
        T121 t121 = new T121();
        int maxProfit = t121.maxProfit(new int[]{7,6,4,3,1});
        System.out.println(maxProfit);
    }
}
